function addcookie(name,value,expireHours){
    var cookieString=name+"="+encodeURIComponent(value)+"; path=/";
    //判断是否设置过期时间
    if(expireHours>0){
        var date=new Date();
        date.setTime(date.getTime+expireHours*3600*1000);
        cookieString=cookieString+"; expire="+date.toGMTString();
    }
    document.cookie=cookieString;
}

function getcookie(name){
    var strcookie=document.cookie;
    var arrcookie=strcookie.split("; ");
    for(var i=0;i<arrcookie.length;i++){
        var arr=arrcookie[i].split("=");
        if(arr[0]==name)return decodeURIComponent(arr[1]);
    }
    return "";
}

function delCookie(name){//删除cookie
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getcookie(name);
    if(cval!=null) document.cookie= name + "="+cval+"; path=/;expires="+exp.toGMTString();
}

function getParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
function extractImages (content) {
    var img_url_base = SITE_BASE + "upload/tieba/smallPic/";
    var img_list = [];
    img_list = content.match(/\[img:([\w\.]+)/g);
    if(img_list===null) {
        //return [];
    	return extractImages2(content);
    } else {
        img_list = img_list.map(function (val) {
            return img_url_base + val.replace(/\[img:/g, '');
        });
    }
    return img_list.slice(0,3);

}
function extractImages2 (content) {
	var img_list = [];
    img_list = content.match(/src=[\w\.:"'/]+[jpegpngif]{3,4}/gi);
    if(img_list===null) {
        return [];
    } else {
        img_list = img_list.map(function (val) {
            return val.replace(/src="/g, '').replace('\"', '');
        });
    }
	return img_list.slice(0,3);
	
}
function getAbstract(content) {
    content  = content.replace(/\[img:[\w\.]+?\]/g,'');
    content = content.substr(0,50);
    var regex = /\[[^\]]+$/;
    if(regex.test(content)) {
        content = content.substring(0, content.lastIndexOf('['));
    }
    return content;
}
function renderContent(content) {
    var regex = /\[img:(.*?)\]/g;
    content = content.replace(regex,'<img src=\"'+SITE_BASE+'/upload/tieba/$1\" class="img">');
    regex = /\[emoji:(.*?)\]/g;
    content = content.replace(regex, '<img src=\"images/tieba/$1\" class="emoji">');
    regex = /\[@:(\w+?)]/g;
    content = content.replace(regex, '<a href=\"chat.html?client=$1\" class=\"at\">@$1</a>');
    content = content.replace(/\n/g,'<br>');
    return content;
}
function isWeixinBrowser(){
    return /micromessenger/.test(navigator.userAgent.toLowerCase())
}

String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {
    if (!RegExp.prototype.isPrototypeOf(reallyDo)) {
        return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);
    } else {
        return this.replace(reallyDo, replaceWith);
    }
}
//当前时间
function currentTime(){
    var d=new Date(),str='';
    str+=d.getFullYear()+'-';
    str+=d.getMonth() + 1+'-';
    str+=d.getDate()+'&nbsp;&nbsp;';
    str+=(d.getHours() > 9 ? d.getHours() : '0'+ d.getHours())+':';
    str+=(d.getMinutes() > 9 ? d.getMinutes() : '0'+ d.getMinutes())+':';
    str+=(d.getSeconds() > 9 ? d.getSeconds() : '0'+ d.getSeconds());
    return str;
}
function stringToDate(str){
    var tempStrs = str.split(" ");
    var dateStrs = tempStrs[0].split("-");
    var year = parseInt(dateStrs[0], 10);
    var month = parseInt(dateStrs[1], 10) - 1;
    var day = parseInt(dateStrs[2], 10);
    var timeStrs = tempStrs[1].split(":");
    var hour = parseInt(timeStrs [0], 10);
    var minute = parseInt(timeStrs[1], 10);
    var second = parseInt(timeStrs[2], 10);
    var date = new Date(year, month, day, hour, minute, second);
    return date;
}
// 判断手机号是否正确
function isPhoneNum(phone) {
    var phoneReg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
    if(!phoneReg.test(phone)){
        return false;
    } else {
        return true;
    }
}
// 判断是否是身份证号
var Wi = [ 7, 9, 10, 5, 8, 4, 2, 1,
    6, 3, 7, 9, 10, 5, 8, 4, 2,
    1 ];// 加权因子;
var ValideCode = [ 1, 0, 10, 9, 8,
    7, 6, 5, 4, 3, 2 ];// 身份证验证位值，10代表X;
function isCertNum(gets, obj, curform, datatype) {
        if (gets.length == 15) {
            return isValidityBrithBy15IdCard(gets);
        } else if (gets.length == 18) {
            var a_idCard = gets.split("");// 得到身份证数组
            if (isValidityBrithBy18IdCard(gets)
                && isTrueValidateCodeBy18IdCard(a_idCard)) {
                return true;
            }
            return false;
        }
        return false;
}

function isTrueValidateCodeBy18IdCard(a_idCard) {
    var sum = 0; // 声明加权求和变量
    if (a_idCard[17].toLowerCase() == 'x') {
        a_idCard[17] = 10;// 将最后位为x的验证码替换为10方便后续操作
    }
    for ( var i = 0; i < 17; i++) {
        sum += Wi[i] * a_idCard[i];// 加权求和
    }
    valCodePosition = sum % 11;// 得到验证码所位置
    if (a_idCard[17] == ValideCode[valCodePosition]) {
        return true;
    }
    return false;
}

function isValidityBrithBy18IdCard(idCard18) {
    var year = idCard18.substring(
        6, 10);
    var month = idCard18.substring(
        10, 12);
    var day = idCard18.substring(
        12, 14);
    var temp_date = new Date(year,
        parseFloat(month) - 1,
        parseFloat(day));
    // 这里用getFullYear()获取年份，避免千年虫问题
    if (temp_date.getFullYear() != parseFloat(year)
        || temp_date.getMonth() != parseFloat(month) - 1
        || temp_date.getDate() != parseFloat(day)) {
        return false;
    }
    return true;
}

function isValidityBrithBy15IdCard(idCard15) {
    var year = idCard15.substring(
        6, 8);
    var month = idCard15.substring(
        8, 10);
    var day = idCard15.substring(
        10, 12);
    var temp_date = new Date(year,
        parseFloat(month) - 1,
        parseFloat(day));
    // 对于老身份证中的你年龄则不需考虑千年虫问题而使用getYear()方法
    if (temp_date.getYear() != parseFloat(year)
        || temp_date.getMonth() != parseFloat(month) - 1
        || temp_date.getDate() != parseFloat(day)) {
        return false;
    }
    return true;
}
var time_range = function (beginTime, endTime) {
    var strb = beginTime.split (":");
    if (strb.length != 2) {
        return false;
    }
    var stre = endTime.split (":");
    if (stre.length != 2) {
        return false;
    }
    var b = new Date ();
    var e = new Date ();
    var n = new Date ();

    b.setHours (strb[0]);
    b.setMinutes (strb[1]);
    e.setHours (stre[0]);
    e.setMinutes (stre[1]);
    if (n.getTime () - b.getTime () > 0 && n.getTime () - e.getTime () < 0) {
        return true;
    } else {
        return false;
    }
}
//显示提示框，目前三个参数(txt：要显示的文本；time：自动关闭的时间（不设置的话默认1500毫秒）；status：默认0为错误提示，1为正确提示；)
function showTips(txt,time,status)
{
    var htmlCon = '';
    if(txt != ''){
        if(status != 0 && status != undefined){
            htmlCon = '<div class="tipsBox" style="width:220px;padding:10px;background-color:#4AAF33;border-radius:4px;-webkit-border-radius: 4px;-moz-border-radius: 4px;color:#fff;box-shadow:0 0 3px #ddd inset;-webkit-box-shadow: 0 0 3px #ddd inset;text-align:center;position:fixed;top:25%;left:50%;z-index:999999;margin-left:-120px;"><img src="front/images/ok.png" style="vertical-align: middle;margin-right:5px;" alt="OK，"/>'+txt+'</div>';
        }else{
            htmlCon = '<div class="tipsBox" style="width:220px;padding:10px;background-color:#D84C31;border-radius:4px;-webkit-border-radius: 4px;-moz-border-radius: 4px;color:#fff;box-shadow:0 0 3px #ddd inset;-webkit-box-shadow: 0 0 3px #ddd inset;text-align:center;position:fixed;top:25%;left:50%;z-index:999999;margin-left:-120px;"><img src="front/images/err.png" style="vertical-align: middle;margin-right:5px;" alt="Error，"/>'+txt+'</div>';
        }
        $('body').prepend(htmlCon);
        if(time == '' || time == undefined){
            time = 1500;
        }
        setTimeout(function(){ $('.tipsBox').remove(); },time);
    }
}